package dao;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import model.VoyageTrain;

@Stateless
public class VoyageTrainDaoImpl implements IVoyageTrainDao {

	@PersistenceContext(unitName = "putravel")
	EntityManager em;

	@Override
	public VoyageTrain find(String GareDepart, String GareArrivee,
			String DateDepart) {
		VoyageTrain result;
		Query req = em
				.createQuery("select v from  VoyageTrain v where v.gareDepart.nom=:depart and "
						+ "v.gareArrivee.nom=:arrivee and v.dateDepart=:date");
		req.setParameter("depart", GareDepart);
		req.setParameter("arrivee", GareArrivee);
		req.setParameter("date", DateDepart);
		try {
			result = (VoyageTrain) req.getSingleResult();
			return result;
		} catch (javax.persistence.NoResultException e) {
			return null;
		}
	}

	@Override
	public void addVoyageTrain(VoyageTrain voyagetrain) {
		em.persist(voyagetrain);
	}

}
